package com.lyhw.model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by phk on 2014/12/25.
 */
public class ImageTag implements Serializable {
    /**
     * 类别名
     */
    private String mCatName;
    /**
     * 内容
     */
    private String mText;
    /**
     * tag所指向的方向
     * 1 为向左
     * 0 为向右
     */
    private int mDir;
    /**
     * tag位置，为百分比
     */
    private float mXP;
    private float mYP;

    public ImageTag(String mCatName, String mText, int mDir, float mXP, float mYP) {
        this.mCatName = mCatName;
        this.mText = mText;
        this.mDir = mDir;
        this.mXP = mXP;
        this.mYP = mYP;
    }

    public void changeDir(){
        mDir = mDir^1;
    }

    public static Label createLabel(ImageTag tag,String author,int order,String typeid){
        return new Label(tag.getmText(),author,tag.getmXP(),tag.getmYP(),order,tag.getmDir(),typeid);
    }

    public static String getTypeIdByTypeName(String typeName){
        //todo typeid转typename
        return typeName;
    }
    public static String getTypeNameByTypeId(String typeId){
        //todo typename转typeid
        return typeId;
    }
    public static ImageTag instanceImageTag(Label label){
        return new ImageTag(getTypeNameByTypeId(label.getTypeid()),
                label.getContent(),label.getDir(),label.getX(),label.getY());
    }

    public static List<ImageTag> labels2tags(List<Label> labels){
        List<ImageTag> list = new ArrayList<>();
        for(Label label : labels){
            list.add(instanceImageTag(label));
        }
        return list;
    }

    public static List<Label> tags2labels(List<ImageTag> tags,String userName){
        List<Label> labels = new ArrayList<>();
        int order=0;
        for (ImageTag tag : tags) {
            labels.add(ImageTag.createLabel(tag, userName, order++, ImageTag.getTypeIdByTypeName(tag.getmCatName())));
        }
        return labels;
    }

    public static List<ImageTag> json2tags(String jsondata){
        Gson gson = new Gson();
        List<ImageTag> tags = gson.fromJson(jsondata,
                new TypeToken<List<ImageTag>>() {
                }.getType());
        return tags;
    }

    public String getmCatName() {
        return mCatName;
    }

    public void setmCatName(String mCatName) {
        this.mCatName = mCatName;
    }

    public String getmText() {
        return mText;
    }

    public void setmText(String mText) {
        this.mText = mText;
    }

    public int getmDir() {
        return mDir;
    }

    public void setmDir(int mDir) {
        this.mDir = mDir;
    }

    public float getmXP() {
        return mXP;
    }

    public void setmXP(float mXP) {
        this.mXP = mXP;
    }

    public float getmYP() {
        return mYP;
    }

    public void setmYP(float mYP) {
        this.mYP = mYP;
    }
}
